<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/23
 * Time: 14:29
 */

//控制器类
namespace Controller;



use Model\IndexModel;

class IndexController extends TooController{

//    后台首页
    public function index(){

        $this->view->display(); //显示页面
    }

    //网站设置
    public function setw(){
        $model=new \Model\IndexModel();
        if($_POST){
           $array=$_POST;
            unset($array['file']); //删除多余项
            //如果没有选择对应图片就不需要修改---过滤数据
            foreach($array as $k=>$v){
                if(empty($v)){
                    unset($array[$k]);
                }
            }


            if($model->set_upd($array)){
                $this->retuAjax(array('type'=>'1','msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>'2','msg'=>'操作失败'));
            }
        }
        else{
            //查询网址设置数据
            $arr=$model->get();
            $arr['banner']=explode('|',$arr['banner']);
            $this->view->assign('arr',$arr);
        }
        $this->view->display();
    }

    //logo 上传
    public function slogo(){
        //接收图片数据
        $file=$_FILES['file']; //图片数据
        $logo=$this->imgFile($file,'/public/Admin/wset/');
        if($logo){
            //返回数据给ajax
            $this->retuAjax(array('code'=>1,'msg'=>'上传成功','data'=>array('src'=>$logo)));
        }
    }

    //栏目管理 列表
    public function category(){
        $model=new IndexModel();
        $sql="select * from sh_category";
        $arr=$model->pdo_sel($sql,2);
        $this->view->assign('arr',$arr);
        $this->view->display();
    }

    //添加栏目
    public function category_add(){
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['sort']=$_POST['sort'];
            $model=new IndexModel();
            if($model->pdo_insert('sh_category',$arr)){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }
        $this->view->display();
    }

    //修改和删除方法
    public function cate_upd(){
        $model=new IndexModel();
        if($_POST){
            $arr=array();
            $arr['name']=$_POST['name'];
            $arr['sort']=$_POST['sort'];
            $model=new IndexModel();
            if($model->pdo_upd('sh_category',$arr," where id = '{$_POST['id']}'")){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }

        $type=$_GET['type'];
        $id=$_GET['id'];
        if($type=='del'){
            //删除
            $sql="delete from sh_category where id in ($id)";
            if($model->pdo_exec($sql)){
                $this->retuAjax(array('type'=>1,'msg'=>'操作成功'));
            }else{
                $this->retuAjax(array('type'=>2,'msg'=>'操作失败'));
            }
        }else{
            //修改
            $list= $model->pdo_sel("select * from sh_category where id = $id limit 1");

            $this->view->assign('arr',$list);
            $this->view->display('category_add');
        }
    }

}